const PREF_PREFIX = "extensions.hypertopic.";
const EXT_NAME = "HTH";
const DEBUG = ""; //logger or dump or ""
const DEFAULT_SERVICE = "http://test.hypertopic.org/argos/";
const DEFAULT_HT_PASS = "pass";
const DEFAULT_HT_USER = "LaSuli User";
const PORPHYRY_PREFIX = "http://porphyry.sf.net/start.php?service=";

const DEFAULT_WEBDAV = "http://argos.webdavserver.com/";
const DEFAULT_WEBDAV_PASS = "pass";
const DEFAULT_WEBDAV_USER = "user";

const CACHE_FILENAME_PREFIX = "hypertopic_cache_";
const HIGHLIGHT_PHPSCRIPT = "http://www.smess.egss.ulg.ac.be/sandbox/highlight.php?";
const FRAGMENT_PHPSCRIPT = "http://www.smess.egss.ulg.ac.be/sandbox/fragment.php?";

const ENTITY_DTD = "http://argos.hypertopic.org/validator/dtd/entity.dtd";
const VIEWPOINTS_DTD = "http://argos.hypertopic.org/validator/dtd/viewpoints.dtd";
const VIEWPOINT_DTD = "http://argos.hypertopic.org/validator/dtd/viewpoint.dtd";
const TOPIC_DTD = "http://argos.hypertopic.org/validator/dtd/topic.dtd";

const COLORS = new Array("hsl(220,62%,74%)", "hsl(44,100%,73%)", "hsl(81,32%,71%)", "hsl(359,73%,78%)", "hsl(260,50%,77%)");
const VIEWPOINT_BG_COLOR = new Array("#C4C4FF","#FF6AB5","#FFFFFF");
const HTMLNS = "http://www.w3.org/1999/xhtml";
const HT_Cc = Components.classes;
const HT_Ci = Components.interfaces;
const MARK_SELECTION_START = '\u200B\u200B\u200B\u200B\u200B';
const MARK_SELECTION_END = '\u200B\u200B\u200B\u200B\u200B\u200B\u200B';

const HIGHLIGHT_TAG = "<!-- highlighted_by_hypertopic_sidebar //-->";

const COLORENTITY_PREFIX = "entity/colorIndex/";

const SCROLL_SCRIPT = '<script language="javascript">'
+ 'function hypertopicScrollTo(elementId) {'
+ ' alert(elementId);'
+ ' var el = document.getElementById(elementId);'
+ '    var valueT = 0, valueL = 0;'
+ '    do {'
+ '      valueT += el.offsetTop  || 0;'
+ '      valueL += el.offsetLeft || 0;'
+ '      el = el.offsetParent;'
+ '    } while (el);'
+ '    window.scrollTo(valueL, valueT);'
+ '}'
+ '</script>';

Array.prototype.sortNum = function() {
   return this.sort( function (a,b) { return a.startPos-b.startPos; } );
}
/*Array.prototype.toJSON = function(){
	for(var i=0,json=[];i<this.length;i++)
		json[i] = (this[i] != null) ? this[i].toJSON() : "null";
	return "["+json.join(", ")+"]"
}*/
// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length; while(i-->b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

/*Object.prototype.toJSON = function(){
	var json = [];
	for(var i in this){
		if(!this.hasOwnProperty(i)) continue;
		//if(typeof this[i] == "function") continue;
		json.push(
			i.toJSON() + " : " +
			((this[i] != null) ? this[i].toJSON() : "null")
		)
	}
	return "{\n " + json.join(",\n ") + "\n}";
}
String.prototype.toJSON = function(){
	return '"' +
		this.replace(/(\\|\")/g,"\\$1")
		.replace(/\n|\r|\t/g,function(){
			var a = arguments[0];
			return  (a == '\n') ? '\\n':
					(a == '\r') ? '\\r':
					(a == '\t') ? '\\t': ""
		}) +
		'"'
}
Boolean.prototype.toJSON = function(){return this}
Function.prototype.toJSON = function(){return this}
Number.prototype.toJSON = function(){return this}
RegExp.prototype.toJSON = function(){return this}
*/
String.prototype.trim = function () {
  var str = this.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
String.prototype.trimNewLine = function () {
  return this.replace(/^\n/, '').replace(/\n$/, '');
}
String.prototype.toXml = function(){
    return this.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
};

String.prototype.startsWith = function(prefix) {
  if (this) return( (this.substring(0, prefix.length) == prefix) ); else return false;
};
String.prototype.endsWith = function(str) {
    return (this.length-str.length)==this.lastIndexOf(str);
};
String.prototype.isURL = function() {
var rx = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?");
var matches = rx.exec(this);
return (matches != null && this == matches[0]); };

var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);
function getURL() {
  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
           getService(Components.interfaces.nsIWindowMediator);
  var recentWindow = wm.getMostRecentWindow("navigator:browser");
  return recentWindow ? recentWindow.content.document.location : null;
}